package com.threeti.mecool.core.domain.model.basicdata.biz;

import java.util.List;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.beans.factory.annotation.Configurable;

import com.threeti.mecool.framework.domain.support.DefaultDomainEntity;

/**
 * 通路相关数据的同步(实体)
 */
@Configurable
@Entity
@Table(name = "DM_CHANNEL_SYNC")
public class ChannelSync extends DefaultDomainEntity {
    public static final int TYPE_CONCERNED_ABOUT_GPS = 0;
    public static final int TYPE_NOT_CONCERNED_ABOUT_GPS = 1;

    @Embedded
    private Channel channel;
    private Double latitude;
    private Double longitude;

    private int type = TYPE_CONCERNED_ABOUT_GPS;
    
    public ChannelSync(Channel channel) {
        super();
        this.channel = channel;
    }

    public ChannelSync(Channel channel, int type) {
        this(channel);
        this.type = type;
    }

    public Channel getChannel() {
        return channel;
    }

    public int getType() {
        return type;
    }

    public String getCode() {
        return channel == null ? "" : channel.getCode();
    }

    public String getName() {
        return channel == null ? "" : channel.getName();
    }

    public String getCategory() {
        return channel == null ? null : channel.getCategory();
    }

    public String getSysCategory() {
        return channel == null ? "" : channel.getSysCategory();
    }

    public Long getAdminDivId() {
        return channel == null ? null : channel.getAdminDivId();
    }

    public String getAddress() {
        return channel == null ? "" : channel.getAddress();
    }

    public String getProvinceAdName() {
        return channel == null ? "" : channel.getProvinceAdName();
    }

    public String getProvinceAdCode() {
        return channel == null ? "" : channel.getProvinceAdCode();
    }

    public String getCityAdName() {
        return channel == null ? "" : channel.getCityAdName();
    }

    public String getCityAdCode() {
        return channel == null ? "" : channel.getCityAdCode();
    }

    public String getAdName() {
        return channel == null ? "" : channel.getAdName();
    }

    public String getAdCode() {
        return channel == null ? "" : channel.getAdCode();
    }

    public Double getLatitude() {
        return latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public ChannelSync() {
    }


    @Override
    public String toString() {
        return "ChannelSync [channel=" + channel + ", id=" + getId() + "]";
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public void setAdName(String adName) {
        if (channel == null) return;
        channel.setAdName(adName);
    }

    public void setAdCode(String adCode) {
        if (channel == null) return;
        channel.setAdCode(adCode);
    }

    public void setAdminDivId(Long adminDivId) {
        if (channel == null) return;
        channel.setAdminDivId(adminDivId);
    }

    public void setCityAdName(String cityAdName) {
        if (channel == null) return;
        channel.setCityAdName(cityAdName);
    }

    public void setCityAdCode(String cityAdCode) {
        if (channel == null) return;
        channel.setCityAdCode(cityAdCode);
    }

    public void setProvinceAdName(String provinceAdName) {
        if (channel == null) return;
        channel.setProvinceAdName(provinceAdName);
    }

    public void setProvinceAdCode(String provinceAdCode) {
        if (channel == null) return;
        channel.setProvinceAdCode(provinceAdCode);
    }

    public void setName(String name) {
        if (channel == null) return;
        channel.setName(name);
    }

    public void setAddress(String addr) {
        if (channel == null) return;
        channel.setAddress(addr);
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }
    
    @Transient
    @PersistenceContext
    EntityManager entityManager;
    
    public String getChannelCatName(){
    	Query q = entityManager.createNativeQuery("select t.pos_kind_name from MD_POS_KIND t where t.pos_kind_code ="+channel.getCategory());
    	List<String> result = q.getResultList();
    	if(result!=null && result.size()==1){
    		return result.get(0);
    	}else{
    		return "";
    	}
    }
    
    public String getChannelSysCatName(){
    	Query q = entityManager.createNativeQuery("select t.pos_sys_type_name from MD_POS_SYS_TYPE t where t.pos_sys_type_code="+channel.getSysCategory());
    	List<String> result = q.getResultList();
    	if(result!=null && result.size()==1){
    		return result.get(0);
    	}else{
    		return "";
    	}
    }
    
    
    
}
